﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class WorldUI : MonoBehaviour
{
    public Transform BoundObject;
    public Vector3 HeightOffset =new Vector3(0, 3f,0);
    public bool follow_camera = false;
    bool Bounded = false;
    public  Text[] Text_childs;
    public Image[] Image_childs;

    public bool screenShort_bool;
    Transform camera朝向;

    private void Awake()
    {
       
    }
    void Start()
    {
        Text_childs = this.transform.GetComponentsInChildren<Text>(true);
        Image_childs = this.transform.GetComponentsInChildren<Image>(true);
        //if (screenShort_bool)
        //{
        //    this.gameObject.layer = 7;
        //    if (UIMain.Instance.jietuCamer == null)
        //    {
        //        UIMain.Instance.jietuCamer = GameObject.Find("截图相机").transform;
        //    }

        //    camera朝向 = UIMain.Instance.jietuCamer.transform;
        //}
        //else
        //{
        //    this.gameObject.layer = 8;
        //}
            camera朝向 = Camera.main.transform;
    }

    // Update is called once per frame
    void LateUpdate()
    {
        //FollowBondObject();
        FollowCamera();
        //ActiveLogic();
    }
     
    void FollowBondObject()
    {
        if (BoundObject == null)
            return;
        Bounded = true;
        this.transform.position = BoundObject.position + HeightOffset;
    }

    void FollowCamera()
    {
        //if (BoundObject == null)
        //    return;
        //if (follow_camera == false)
        //    return;



        //this.transform.rotation = Quaternion.Euler(new Vector3(0,camera朝向.rotation.y));
        this.transform.rotation = camera朝向.rotation;
    }
    float logic_time=0;
    /// <summary>
    /// 每秒刷新一次状态 
    /// </summary>
    void ActiveLogic()
    {
        logic_time += Time.deltaTime;
        if (logic_time < 1)
            return;
        logic_time = 0;

        if (Bounded && BoundObject == null)
        {
            Destroy(this.gameObject);
            return;
        }
        else if (BoundObject == null)
            return;

        if (BoundObject.gameObject.activeSelf == false || BoundObject.parent.gameObject.activeSelf == false)
            EnableUI(false);
        else
            EnableUI(true);

    }

    void EnableUI(bool enable)
    {
        for (int i = 0; i < Text_childs.Length; i++)
            Text_childs[i].enabled = enable;
        for (int i = 0; i < Image_childs.Length; i++)
            Image_childs[i].enabled = enable;
    }
}
